package com.example.controller;

import com.example.dto.AssignAuthDto;
import com.example.entity.Auth;
import com.example.entity.CurrentUser;
import com.example.entity.Result;
import com.example.entity.Role;
import com.example.page.Page;
import com.example.service.AuthService;
import com.example.service.RoleService;
import com.example.utils.TokenUtils;
import com.example.utils.WarehouseConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author:tjf
 * @Description:
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private TokenUtils tokenUtils;
    @Autowired
    private AuthService authService;

    /**
     * 获取角色列表的接口url/role/role-list
     * @return
     */
    @RequestMapping("/role-list")
    public Result getAllRoles(){
        List<Role> roleList = roleService.queryAllRoles();
        return Result.ok(roleList);
    }

    /**
     * 根据条件分页查询角色列表接口url/role/role-page-list
     * @return
     */
    @RequestMapping("/role-page-list")
    public Result queryRoleByConditionForPagination(Role role, Page page){
        // 执行分页查询的业务方法
        roleService.queryRoleByConditionForPagination(role, page);
        // 响应结果page
        return Result.ok(page);
    }

    /**
     * 添加角色接口url/role/role-add
     * @param role
     * @param token
     * @return
     */
    @RequestMapping("/role-add")
    public Result saveRole(@RequestBody Role role,
                           @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token){
        // 获取当前登录用户的信息
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);

        // 二次封装role
        role.setCreateBy(currentUser.getUserId());

        // 执行添加role的业务方法并响应结果result
        return roleService.saveRole(role);
    }

    /**
     * 启用或禁用角色接口url/role/role-state-update
     * @param role
     * @param token
     * @return
     */
    @RequestMapping("/role-state-update")
    public Result modifyRoleStateByRoleId(@RequestBody Role role,
                            @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token){
        // 获取当前登录用户的信息
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);

        // 二次封装role
        role.setCreateBy(currentUser.getUserId());

        // 执行修改roleState的业务方法并响应Result
        return roleService.modifyRoleStateByRoleId(role);
    }

    /**
     * 根据roleId查询authId接口url/auth/role-auth
     * @param roleId
     * @return
     */
    @RequestMapping("/role-auth")
    public Result queryAuthIdByRoleId(int roleId){
        // 执行根据roleId查询authId的业务方法
        List<Integer> authList = authService.queryAuthIdByRoleId(roleId);

        return Result.ok(authList);
    }

    /**
     * 给roleId分配权限接口url/role/auth-grant
     * @return
     */
    @RequestMapping("/auth-grant")
    public Result grantAuthByRoleId(@RequestBody AssignAuthDto assignAuthDto){
        roleService.grantAuthByRoleId(assignAuthDto.getRoleId(), assignAuthDto.getAuthIds());

        return Result.ok("权限修改成功！");
    }

    /**
     * 根据角色id删除角色接口url/role/role-delete/{roleId}
     * @param roleId
     * @return
     */
    @RequestMapping("/role-delete/{roleId}")
    public Result removeRoleByRoleId(@PathVariable("roleId") int roleId){
        // 执行根据角色id删除角色的业务方法
        roleService.removeRoleByRoleId(roleId);

        return Result.ok("角色删除成功！");
    }

    /**
     * 根据roleId修改role信息接口url/role/role-update
     * @param role
     * @param token
     * @return
     */
    @RequestMapping("/role-update")
    public Result modifyRoleDescByRoleId(@RequestBody Role role,
                         @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token){
        // 获取当前登录用户的信息
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);

        // 二次封装role
        role.setUpdateBy(currentUser.getUserId());

        // 执行修改role信息的业务方法并响应结果
        return roleService.modifyRoleDescByRoleId(role);
    }
}
